/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import dk.redia.lib.log.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import dk.redia.libraryapp.activityutils.LibraryAppActivity;
import dk.redia.libraryapp.adapters.LibraryAdapter;
import dk.redia.libraryapp.model.Library;
import dk.redia.libraryapp.model.LibraryModel;
import dk.redia.libraryapp.model.LibraryModelListener;
import dk.redia.libraryapp.utils.ResourceUtils;

public class OpeningHoursLibraryListActivity extends LibraryAppActivity {

	private LibraryAdapter libraryAdapter;
	
	private static final String logTag = "OpeningHoursLibraryListActivity";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.libraryopeninghourslist);
    	libraryAdapter = new LibraryAdapter(this);
    	ImageView wideLogoImageView = (ImageView)findViewById(R.id.libraryOpeningHoursWideLogoImageView);
	    wideLogoImageView.setImageResource(ResourceUtils.getWideLogoResourceId());
    	ListView lv = (ListView)findViewById(R.id.libraryOpeningHoursListView);
    	lv.setAdapter(libraryAdapter);
    	
    	load();
	}

	private void loading(boolean loading) {
		findViewById(R.id.libraryListProgressBar).setVisibility(loading ? View.VISIBLE : View.GONE);
	}
	
	public void onItemClicked(View v) {
		Object o = v.getTag();
		Library l = null;
		if (o instanceof Library) {
			l = (Library)o;
			Log.d(OpeningHoursLibraryListActivity.logTag, "Showing details for library " + l.getName());
		} else {
			Log.e(OpeningHoursLibraryListActivity.logTag, "Unable to convert tag data to library instance");
		}
		Intent intent = new Intent(this, LibraryDetailsActivity.class);
		intent.putExtra(LibraryDetailsActivity.extraKeyLibrary, l);
		Log.d(OpeningHoursLibraryListActivity.logTag, "Item clicked");
		startActivity(intent);
	}
		
	private void load() {
		
		libraryAdapter.clear();
		loading(true);
		
		LibraryModel.getInstance().getLibraries(new LibraryModelListener() {
			public void getLibrariesResult(List<Library> libraries) {
				Log.d(OpeningHoursLibraryListActivity.logTag, "Found " + libraries.size() + " libraries");
				loading(false);
				libraryAdapter.addAll(libraries);
			}
			
			public void getLibrariesFailure(String message) { 
				loading(false);
				Log.w(OpeningHoursLibraryListActivity.logTag, "Failed to get libraries from model");
			}
		});
	}
}
